﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class PostExamRoom : System.Web.UI.Page
    {
        private int editid = 0, delid = 0, page = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "ExamroomEdit"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (!PageValidate.QueryStringIsEmpty("editid"))
            {
                editid = Convert.ToInt32(PageValidate.QueryString("editid"));
            }
            if (!PageValidate.QueryStringIsEmpty("delid"))
            {
                delid = Convert.ToInt32(PageValidate.QueryString("delid"));
            }
            if (!PageValidate.QueryStringIsEmpty("page"))
            {
                page = Convert.ToInt32(PageValidate.QueryString("page"));
            }
            if (!IsPostBack)
            {
                BindExamPaper();
                BindArea();
                if (editid > 0)
                {
                    ExamRoomInfo examRoomInfo = new ExamRoomInfo();
                    examRoomInfo = ExamRoom.GetSingleInfo(editid);
                    this.ExamName.Text = examRoomInfo.ExamName;
                    this.StartDate.Text = examRoomInfo.ExamStartDate != null ? examRoomInfo.ExamStartDate.Value.ToShortDateString(): "";
                    this.EndDate.Text = examRoomInfo.ExamEndDate != null ? examRoomInfo.ExamEndDate.Value.ToShortDateString() : "";
                    this.ExamPaper.SelectedValue = examRoomInfo.ExamPaperId.ToString();
                    if (examRoomInfo.ExamAreaId != null)
                        CheckBoxListHelper.SetSelectedValue(this.ExamArea, examRoomInfo.ExamAreaId.ToString());
                }
                if (delid > 0)
                {
                    if (!UserValidate.CheckLogin(this.Request, "ExamroomEdit"))
                    {
                        Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                        Response.Redirect("login.aspx");
                        Response.End();
                    }
                    StudentScore.DelByExamRoomId(delid);
                    ExamRoom.DelInfo(delid);
                    Response.Redirect("AdminExamRoom.aspx?page=" + page);
                    return;
                }
            }
        }
        protected void BindExamPaper()
        {
            IList<ExamPaperInfo> examPaperInfos = BAL.ExamPaper.GetList();
            ExamPaper.Items.Clear();
            foreach (ExamPaperInfo info in examPaperInfos)
            {
                this.ExamPaper.Items.Add(new ListItem(info.Name.ToString(), info.Id.ToString()));
            }
        }
        protected void BindArea()
        {
            IList<AreaInfo> areaInfos = Area.GetList();
            ExamArea.Items.Clear();
            foreach (AreaInfo info in areaInfos)
            {
                ExamArea.Items.Add(new ListItem(info.Name.ToString(), info.Id.ToString()));
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (PageValidate.IsEmpty(ExamName.Text.Trim()))
            {
                Utility.ShowJsAlert("考试名称不能为空！", this);
                return;
            }
            if (PageValidate.FormIsEmpty("StartDate") || PageValidate.FormIsEmpty("EndDate"))
            {
                Utility.ShowJsAlert("没有选择考试开始或结速时间！", this);
                return;
            }
            ExamRoomInfo examRoomInfo = null;
            if (editid > 0)
            {
                examRoomInfo = ExamRoom.GetSingleInfo(editid);
                if (examRoomInfo == null)
                {
                    Utility.ShowJsAlert("非法数据错误！", this);
                    return;
                }
            }
            else
            {
                examRoomInfo = new ExamRoomInfo();
            }
            examRoomInfo.ExamName = this.ExamName.Text.Trim();
            examRoomInfo.ExamStartDate = DateTime.Parse(PageValidate.Form("StartDate"));
            examRoomInfo.ExamEndDate = DateTime.Parse(PageValidate.Form("EndDate"));
            examRoomInfo.ExamPaperId = int.Parse(this.ExamPaper.SelectedItem.Value);
            examRoomInfo.ExamPaperName = this.ExamPaper.SelectedItem.Text;
            examRoomInfo.SubjectId = BAL.ExamPaper.GetT1(examRoomInfo.ExamPaperId).Subject.Value;
            examRoomInfo.ExamAreaId = CheckBoxListHelper.SelectedValue(this.ExamArea);
            examRoomInfo.ExamAreaName = CheckBoxListHelper.SelectedText(this.ExamArea);
            ExamRoom.Save(examRoomInfo);
            Utility.ReloadTopPage(this);
        }
    }
}
